//
//  YbrMyBagDao.m
//  wifibox
//
//  Created by os X 10.9 on 14-11-21.
//  Copyright (c) 2014年 yinbaner. All rights reserved.
//

#import "YbrMyBagDao.h"

@implementation YbrMyBagDao

// 插入一个课程到我的书包
-(BOOL) insertCourse:(YbrHttpMyBagItem *) myBag {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdate:@"insert into my_bag (name ,courseid, img ,channelid ,channelname ,channeltype,musicstr,comeFrom) VALUES (?,?,?,?,?,?,?,?)",
                         myBag.courseName ,
                         [NSNumber numberWithInt:myBag.courseId] ,
                         myBag.img ,
                         [NSNumber numberWithInt:myBag.channelId] ,
                         myBag.channelName ,
                         [NSNumber numberWithInt:myBag.channelType] ,
                         myBag.musicContent,
                         [NSNumber numberWithInt:myBag.comeFrom]];
    }];
    
    return isOk;
}

// 是否插入书包
-(BOOL) isAddtoMyBag:(int) courseId {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from my_bag where courseid=%i",courseId];
        if(rs.next) {
            isOk = true;
        }
        [rs close];
    }];
    
    return isOk;
}

// 获得所有的课程 (NSArray里面放的是课程的列表  YbrHttpMyBagItem)
-(NSArray *) getAllCourse {
    __block NSArray *arr;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from my_bag"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrHttpMyBagItem *item  = [[YbrHttpMyBagItem alloc] init];
            item.courseName         = [rs stringForColumn:@"name"];
            item.courseId           = [rs intForColumn:@"courseid"];
            item.img                = [rs stringForColumn:@"img"];
            item.channelId          = [rs intForColumn:@"channelid"];
            item.channelName        = [rs stringForColumn:@"channelname"];
            item.channelType        = [rs intForColumn:@"channeltype"];
            item.comeFrom           = [rs intForColumn:@"comeFrom"];
            [tempArr addObject:item];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    
    return arr;
}

// 删除很多的课程
-(BOOL) removeCourses:(NSArray *) courseIds {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for (NSNumber *ID in courseIds) {
            isOk = [db executeUpdateWithFormat:@"delete from my_bag where courseid=%i",[ID intValue]];
        }
    }];
    
    return isOk;
}


// 删除课程
-(BOOL) deleCourseById:(int) courseId {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from my_bag where courseid=%i",courseId];
    }];
    return isOk;
}


// 获得音频的内容根据音频的ID
-(NSString *) getMusicStrByCourseId:(int) courseId {
    __block NSString *musicContent;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        rs = [db executeQueryWithFormat:@"select musicstr from my_bag where courseid=%i" ,courseId];
        
        while (rs.next) {
            musicContent = [rs stringForColumn:@"musicstr"];
        }
        
        [rs close];
    }];
    return musicContent;
}


// 根据课程的ID获得课程
-(YbrHttpMyBagItem *) getCourseInfoById:(int) courseId {
    __block YbrHttpMyBagItem *myBagItem;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from my_bag where courseid=%i" ,courseId];
        
        while (rs.next) {
            YbrHttpMyBagItem *item = [[YbrHttpMyBagItem alloc] init];
            item.courseName = [rs stringForColumn:@"name"];
            item.courseId = [rs intForColumn:@"courseid"];
            item.channelId = [rs intForColumn:@"channelid"];
            item.channelName = [rs stringForColumn:@"channelname"];
            item.channelType = [rs intForColumn:@"channeltype"];
            
            myBagItem = item;
            break;
        }
        [rs close];
    }];
    return myBagItem;
}
@end